#include "OgreAMLPlugin.h"

#include <OgreMeshDefinitionManager.h>
#include <OgreException.h>

namespace Ogre
{

//-------------------------------------------------------------------------------------------------------------------------------------------------
AMLPlugin::AMLPlugin()
: mMeshDefinitionManager(NULL)
{
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
AMLPlugin::~AMLPlugin()
{
	OgreAssert( !mMeshDefinitionManager, "Application bug, plugin not destroyed properly" );
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
const String& AMLPlugin::getName() const
{
	static const String sPluginName = "Advanced Mesh Loader";
	return sPluginName;
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
void AMLPlugin::install()
{
	OgreAssert( !mMeshDefinitionManager, "Application bug, plugin already installed" );

	// Create new manager instance
	mMeshDefinitionManager = OGRE_NEW_T( MeshDefinitionManager, MEMCATEGORY_GENERAL )();
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
void AMLPlugin::initialise()
{
	assert( mMeshDefinitionManager && "Application bug, plugin is not installed" );
	
	// Is manager exist?
	if( !mMeshDefinitionManager )
	{
		OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
			"Application bug, plugin is not installed",
			"AMLPlugin::initialise" );
	}

	mMeshDefinitionManager->_initialise();
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
void AMLPlugin::shutdown()
{
	assert( mMeshDefinitionManager && "Application bug, plugin is not installed" );
	
	// Is manager exist?
	if( !mMeshDefinitionManager )
	{
		OGRE_EXCEPT( Exception::ERR_INVALIDPARAMS,
			"Application bug, plugin is not installed",
			"AMLPlugin::shutdown" );
	}

	mMeshDefinitionManager->_shutdown();
}

//-------------------------------------------------------------------------------------------------------------------------------------------------
void AMLPlugin::uninstall()
{
	OgreAssert( mMeshDefinitionManager, "Application bug, plugin is not installed" );

	// Destroy existing instance
	OGRE_DELETE_T( mMeshDefinitionManager, MeshDefinitionManager, MEMCATEGORY_GENERAL );
	mMeshDefinitionManager = NULL;
}

} // end of namespace Ogre
